<?php

namespace common\controller;

// +----------------------------------------------------------------------
// | 控制器基类，包含
// +----------------------------------------------------------------------

use teamones\etcd\Discovery;
use think\Controller;
use think\Request;
use think\module\Module;
use common\service\FieldService;
use common\service\CommonService;

class VerifyController extends Controller
{
    /**
     * 字段基础方法
     * @return \think\Response
     */
    public function fields(Request $request)
    {
        $fieldService = new FieldService();
        $fieldsData = $fieldService->getFieldConfig($request->moduleCode);
        return json(success_response('', $fieldsData));
    }


    /**
     * 单条数据查找基础方法
     * @param Request $request
     * @return \think\Response
     */
    public function find(Request $request)
    {
        $param = $request->param();
        $instance = CommonService::instance($request->controller());
        $resData = $instance->find($param['param'], $request->moduleCode);
        return json(success_response('', $resData));
    }

    /**
     * 多条数据查询基础方法
     * @param Request $request
     * @return \think\Response
     */
    public function select(Request $request)
    {
        $param = $request->param();
        $instance = CommonService::instance($request->controller());
        $resData = $instance->select($param['param'], $request->moduleCode);
        return json(success_response('', $resData));
    }

    /**
     * 创建基础方法
     * @param Request $request
     * @return \think\Response
     */
    public function create(Request $request)
    {
        $param = $request->param();
        $instance = CommonService::instance($request->controller());
        $param['module_code'] = $request->moduleCode;
        $instance->setCurrentModuleCode($request->moduleCode);
        $resData = $instance->commonCreate($param, Module::$moduleDictData['module_index_by_code'][$request->moduleCode]);
        return json(success_response('', $resData));
    }

    /**
     * 创建基础方法
     * @param Request $request
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $instance = CommonService::instance($request->controller());
        $throwNoDataUpdateException = $request->param()['throw_no_data_change_exception'] ?? 'yes';
        $throwNoDataUpdateException = $throwNoDataUpdateException === 'yes';
        $resData = $instance->commonUpdate($request->param(), $throwNoDataUpdateException);
        return json(success_response('', $resData));
    }

    /**
     * 删除基础方法
     * @param Request $request
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        $instance = CommonService::instance($request->controller());
        $resData = $instance->delete($request->param());
        return json(success_response('', $resData));
    }
}
